﻿using System;
using UnityEngine;
using Game.Logic;
using Game.Logic.Manager;
using NetFramework;

public class DebugInfo : MonoBehaviour
{
    private float m_LastUpdateShowTime = 0f;        //上一次更新帧率的时间;
    private float m_UpdateShowDeltaTime = 0.5f;     //更新帧率的时间间隔;
    private int m_FrameUpdate = 0;                  //帧数;

    private float m_FPS = 0.0f;
    private float m_LAG = 0.0f;

    GUIStyle Style;

    public string IP = "aliner.oicp.io";
    Ping ping;

    void Awake()
    {
        Application.targetFrameRate = 60;

        GatewayInfo? info = GameGatewayManager.Instance.GetCurGatewayInfo();
        IP = info?.addr;

        // Style = new GUIStyle();
        // Style.fontSize = 14;
        // Style.normal.textColor = new Color(46f / 256f, 163f / 256f, 256f / 256f, 256f / 256f);
        // Style.Draw
        // Style.normal.background = new Color(46f / 256f, 163f / 256f, 256f / 256f, 256f / 256f);
    }

    // Use this for initialization
    void Start()
    {
        m_LastUpdateShowTime = Time.realtimeSinceStartup;
    }

    // Update is called once per frame
    void Update()
    {
        m_FrameUpdate++;

        m_LAG = NetworkManager.Instance.GetNetLag();
        if (m_LAG <= 0 && null != ping && ping.isDone)
        {
            m_LAG = ping.time;
            ping.DestroyPing();
            ping = null;
        }   

        if (Time.realtimeSinceStartup - m_LastUpdateShowTime < m_UpdateShowDeltaTime)
            return;
        m_FPS = (float)Math.Floor(m_FrameUpdate / (Time.realtimeSinceStartup - m_LastUpdateShowTime) * 100) / 100;
        m_FrameUpdate = 0;
        m_LastUpdateShowTime = Time.realtimeSinceStartup;

        if (null == ping)
            SendPing();
    }

    void OnGUI()
    {
        GUI.Box(new Rect(10, 10, 100, 20), "FPS: " + m_FPS);
        GUI.Box(new Rect(10, 30, 100, 20), "LAG: " + m_LAG);
    }

    void SendPing()
    {
        ping = new Ping(IP);
    }
}

